package cn.qingyun.gis.modules.init.utils;

import cn.hutool.core.util.StrUtil;
import cn.qingyun.gis.modules.init.system.vo.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author zhoudeli
 * @Date 2021/8/16 12:05
 * @Description 导出工具
 * @ClassName ExportUtil
 **/
@Slf4j
public class ExportUtil {

    public static <T> ModelAndView export(List<T> recordList, Class<T> clazz, String title) {
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject("fileName", title);
        mv.addObject("entity", clazz);
        mv.addObject("params", new ExportParams(title + "报表", "导出人:" + getCurrentUser(), title));
        mv.addObject("data", recordList);
        return mv;
    }

    private static String getCurrentUser() {
        try {
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            return sysUser.getRealname();
        } catch (Exception e) {
            log.debug("用户未登录");
            return StrUtil.EMPTY;
        }
    }
}
